package lab4;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet(name = "CalServlet", value = "/CalServlet")
public class CalServlet extends HttpServlet {
    public int add(int firstVal, int secondVal){
        return firstVal + secondVal;
    }

    public int subtract(int firstVal, int secondVal){
        return firstVal - secondVal;
    }
    public int multiply(int firstVal, int secondVal){
        return firstVal * secondVal;
    }
    public int divide(int firstVal, int secondVal){
        if (secondVal == 0){

        }
        return firstVal / secondVal;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String firstVal = req.getParameter("firstVal");
        String secondVal = req.getParameter("secondVal");
        String name = req.getParameter("name");
        String result = req.getParameter("Result");
        String length = req.getParameter("length");
        Object add = req.getParameter("+");
        Object sub = req.getParameter("-");
        Object mul = req.getParameter("*");
        Object div = req.getParameter("/");
        Object computeLength= req.getParameter("computeLength");
        Object reset= req.getParameter("reset");
//        int result = 0;
//        int length = 0;
        resp.setContentType("text/html; charset=utf-8");
        if (add != null){
            if (StringUtils.isNumeric(firstVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'firstVal is not a Number')");
            }else if (StringUtils.isNumeric(secondVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'secondVal is not a Number')");
            }
            else{
                result =String.valueOf(add(Integer.valueOf(firstVal), Integer.valueOf(secondVal))) ;
            }
        }
        else if (sub != null){
            if (StringUtils.isNumeric(firstVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'firstVal is not a Number')");
            }else if (StringUtils.isNumeric(secondVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'secondVal is not a Number')");
            }
            else{
                result = String.valueOf(subtract(Integer.valueOf(firstVal), Integer.valueOf(secondVal)));
            }
        }
        else if (mul != null){
            if (StringUtils.isNumeric(firstVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'firstVal is not a Number')");
            }else if (StringUtils.isNumeric(secondVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'secondVal is not a Number')");
            }
            else{
                result = String.valueOf(multiply(Integer.valueOf(firstVal), Integer.valueOf(secondVal)));
            }
        }
        else if (div != null){
            if (StringUtils.isNumeric(firstVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'firstVal is not a Number')");
            }else if (StringUtils.isNumeric(secondVal) == false){
                resp.getWriter().println("alert(localhost:8080 says<br/>'secondVal is not a Number')");
            }
            else{
                result = String.valueOf(divide(Integer.valueOf(firstVal), Integer.valueOf(secondVal)));
            }
        }
        else if (computeLength != null){
            if (StringUtils.isNumeric(name) == true){
                resp.getWriter().println("alert(localhost:8080 says<br/>'name is not valid')");
            }
            else{
                length = String.valueOf(name.length());
            }
        }else if (reset != null){
            result= length = firstVal = secondVal =name = "";
            Cookie c1=new Cookie("cResult",result);
            Cookie c2=new Cookie("cLength",length);
            Cookie c3=new Cookie("cfirstVal",firstVal);
            Cookie c4=new Cookie("csecondVal",secondVal);
            Cookie c5=new Cookie("cname",name);
//            System.out.println("name="+name);
            resp.addCookie(c1);
            resp.addCookie(c2);
            resp.addCookie(c3);
            resp.addCookie(c4);
            resp.addCookie(c5);
            resp.sendRedirect("lab4/cal.jsp");
            return;
        }
        System.out.println(result);
       // req.setAttribute("result", result);
      //  req.getRequestDispatcher("").forward(req,resp);
        Cookie c1=new Cookie("cResult",""+result);
        Cookie c2=new Cookie("cLength",""+length);
        Cookie c3=new Cookie("cfirstVal",""+firstVal);
        Cookie c4=new Cookie("csecondVal",""+secondVal);
        Cookie c5=new Cookie("cname",""+name);
        resp.addCookie(c1);
        resp.addCookie(c2);
        resp.addCookie(c3);
        resp.addCookie(c4);
        resp.addCookie(c5);
        resp.sendRedirect("lab4/cal.jsp");
        return;
    }
}
